package com.blueocean_health.care.management.controller;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.blueocean_health.care.management.common.base.em.TimeType;
import com.blueocean_health.care.management.common.base.time.TimeUtils;
import com.blueocean_health.care.management.common.excel.ExcelUtil;
import com.blueocean_health.care.management.domain.base.SysResult;
import com.blueocean_health.care.management.domain.dto.ManyToManyPercentagesDto;
import com.blueocean_health.care.management.domain.vo.ManyToManyDvo;
import com.blueocean_health.care.management.transfer.client.ManyToManyPercentClientService;

/**
 * 用户模块  2018年7月14日14:45:17
 * @author wuyanli
 *
 */
@RestController
@RequestMapping("/manytomany/")
public class ManyToManyController {
	
	@Autowired
	private ManyToManyPercentClientService manyService;
	
	/**
	 * 多对多提成报表  2018年5月25日16:24:38
	 * @return
	 */
	@RequestMapping("percentages/list")
	public SysResult getPercentagesList(@RequestBody ManyToManyDvo manyDvo){
		
		SysResult percentagesList = manyService.getPercentagesList(manyDvo);
		return percentagesList;
		
	}
	
	/**
	 * 导出多对多提成表 Excel  2018年6月1日16:41:44
	 * @param map
	 * @return
	 */
	@RequestMapping("percentages/excel")
	public void getPercentagesListExcel(HttpServletRequest request,HttpServletResponse response){
		try{
			String startTime = request.getParameter("startTime");
			String endTime = request.getParameter("endTime");
			String groupId = request.getParameter("groupId");
			String searchKey = request.getParameter("searchKey");
			String hospitalId = request.getParameter("teamId");
			
			ManyToManyDvo manyDvo = new ManyToManyDvo();
			manyDvo.setStartTime(startTime);
			manyDvo.setEndTime(endTime);
			manyDvo.setGroupId(groupId);
			manyDvo.setTeamId(hospitalId);
			manyDvo.setSearchKey(searchKey);
			List<ManyToManyPercentagesDto> listExcel = manyService.getPercentagesListExcel(manyDvo);
			String finalFileName = URLEncoder.encode("多对多提成表_"+TimeUtils.getTime(TimeType.yyyyMMddHHmmssSSS)+".xls", "UTF8");
			response.addHeader("Content-Disposition", "attachment;filename=" + finalFileName);
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			
			ExcelUtil.getInstance().exportObj2Excel(toClient, listExcel, ManyToManyPercentagesDto.class);
		}catch(IOException e){
			e.printStackTrace();
		}
		
	
	}

	
}
